package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.core.domain.aggregatemodel.Labor;
import com.smedi.ismedi.inmail.core.domain.dto.LaborQuery;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.LaborRemoteApi;
import com.smedi.ismedi.labor.api.dto.request.LaborListRequest;
import com.smedi.ismedi.labor.api.dto.response.LaborBasicResponse;
import com.smedi.ismedi.labor.api.feign.RemoteLaborCoreFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class LaborRemoteApiImpl implements LaborRemoteApi {
    private final RemoteLaborCoreFeignApi remoteLaborCoreFeignApi;
    private final AdapterConverter adapterConverter;

    public List<Labor> getLaborList(LaborQuery laborQuery){
        LaborListRequest laborListRequest = adapterConverter.laborQueryToLaborListRequest(laborQuery);
        ResponseData<List<LaborBasicResponse>> responseData = remoteLaborCoreFeignApi.getLaborList(laborListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return adapterConverter.laborBasicResponseToLabor(responseData.getData());
    }

    @Override
    public Integer getLaborCount(LaborQuery laborQuery) {
        LaborListRequest laborListRequest = adapterConverter.laborQueryToLaborListRequest(laborQuery);
        ResponseData<Integer> responseData = remoteLaborCoreFeignApi.getLaborCount(laborListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }
}
